package com.yuuzy.qqclient.service;

import com.yuuzy.qqcommon.Message;
import com.yuuzy.qqcommon.MessageType;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;

/**
 * 该类完成文件传输服务
 */
public class FileClientService {
	/**
	 * @param src      源文件
	 * @param dest     发送到对方的哪个目录
	 * @param getterID 接受者id
	 * @param senderID 发送者id
	 */
	public static void sendFileToOne(String src, String dest, String getterID, String senderID) throws Exception {
		//读取src文件
		Message message = new Message();
		message.setMesType(MessageType.MESSAGE_FILE_MES);
		message.setGetter(getterID);
		message.setSender(senderID);
		message.setSrc(src);
		message.setDest(dest);

		//需要将源文件读取到message中
		File file = new File(src);
		byte[] bytes = new byte[(int) file.length()];
		FileInputStream fileInputStream = new FileInputStream(file);
		fileInputStream.read(bytes);
		message.setFileBytes(bytes);
		message.setFileLen(bytes.length);
		fileInputStream.close();
		//提示信息

		System.out.println("\n" + getterID + "给" + senderID + "发送文件" + src
				+ "到对方电脑的目录" + dest);
		//向服务器发送文件
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.
				getClientConnectserverThread(senderID).getSocket().getOutputStream());
		objectOutputStream.writeObject(message);
	}
}
